<?php

class WebUser extends CWebUser
{
    public $message;
    protected $cart, $model, $id, $name, $login, $role;


    private static  $rolesNames=array(
        0=>'banned',
        1=>'guest',
        2=>'user',
        3=>'admin',
    );

    public function init()
    {
        $this->cart=Yii::app()->cart;
        $this->model=new User();

        if($this->isGuest){
            $this->role=1;
            $this->setState('role',$this->role);
        }

        parent::init();
    }

    public function getRole()
    {
        if($this->role===null) $this->role=$this->getState('role');
        return $this->role;
    }

    public function getCart()
    {
        return $this->cart;
    }

    public function getId()
    {
        if(!$this->id){
            return $this->id=$this->getState('id');
        }
        return $this->id;
    }

    public function getName()
    {
        if(!$this->name){
            return $this->name=$this->getState('name');
        }
        return $this->name;
    }

    public function getLogin()
    {
        if(!$this->login){
            return $this->login=$this->getState('login');
        }
        return $this->name;
    }

    public function getImage()
    {
        return $this->getModel()->image;
    }

    public function getModel()
    {
        if(!$this->isGuest){
            return $this->model===null || !$this->model->id ? $this->model=$this->model->findByPk($this->getId()) : $this->model;
        }
        return $this->model;
    }

    public function register($data=array())
    {
        $this->model->attributes=$data;
        if($this->model->save()){
            return $this->loginUser($data);
        }
        return false;
    }

    public function loginUser($data=array())
    {
        if(is_object($data) && get_class($data)==get_class($this->getModel())){
            $this->setUserData($data);
            $identity=new CUserIdentity($data->name,$data->password);
            $this->login($identity);
            return true;
        }

        if(!isset($data['login']) || !isset($data['password'])) return false;
        $name=$data['login'];
        $password=$data['password'];
        $remember=isset($data['remember']) ? $data['remember'] : 0;

        if(!$model=$this->model->findByAttributes(array('login'=>$name))){
            $model=$this->model->findByAttributes(array('email'=>$name));
        }

        if($model===null || $model->password!==hash('md5', $model->email.$password.Yii::app()->params['salt'])){
            $this->message=Yii::t('account/customer', 'errorNoSuchUser');
            return false;
        }else{
            if($remember){
                $confirmCode=$this->getConfirmCode();
                Yii::app()->request->cookies['userId']=new CHttpCookie('userId', $model->id);
                Yii::app()->request->cookies['userConfirmCode']=new CHttpCookie('userConfirmCode', $confirmCode);
                $model->confirmCode=$confirmCode;

                $model->update(false);
            }

            $this->setUserData($model);

            $identity=new CUserIdentity($name,$password);
            $this->login($identity);

            return true;
        }
    }

    public function logoutUser()
    {
        unset(Yii::app()->request->cookies['userId']);
        unset(Yii::app()->request->cookies['userConfirmCode']);
        $user=$this->getModel();
        $user->confirmCode=null;
        $user->update(false);
        $this->logout();
    }

    public function checkAccess($roleName,$params=array())
    {
        $role=$this->getModel()->role;
        if($role===null) $role=1;
        $roleId=10;
        foreach(self::$rolesNames as $id=>$name){
            if($name==$roleName){
                $roleId=$id;
                break;
            }
        }

        return $role>=$roleId;
    }

    protected function setUserData($data)
    {
        $this->model=$data;
        $this->id=$data->id;
        $this->name=$data->name;
        $this->login=$data->login;
        $this->role=self::$rolesNames[$data->role];

        $this->setState('id',$this->id);
        $this->setState('name',$this->name);
        $this->setState('login',$this->login);
        $this->setState('role',$this->role);
    }

    protected function getConfirmCode()
    {
        return hash('md5', time().$this->login.Yii::app()->params['salt']);
    }
}